import { ObjResettingWithEvent } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { Destroyable, HasOwner, react,reactJson} from "xbsj-xe2/dist-node/xe2-base-utils";
import { MyProjectManager } from "../MyProjectManager";
import { reqGetMonomer } from '@/api/modelser2Api';

class PickResettting extends Destroyable {
    constructor(monomerEdit: MonomerEdit, owner: MyProjectManager) {
        super();
        const viewer = owner.activeViewer
        if (!viewer) return;
        this.dispose(viewer.clickEvent.don(async e => {
            if (!e.screenPosition) return;
            // 拾取
            const obj = await viewer.pickPosition(e.screenPosition) as [number, number, number]
            console.log("obj", obj);
            await monomerEdit.getMonomer(obj)

        }))
    }
}

export class MonomerEdit extends HasOwner<MyProjectManager> {

    // 开启拾取
    private _enablePick = this.dv(react<boolean>(false));
    get enablePick() { return this._enablePick.value; }
    get enablePickChanged() { return this._enablePick.changed; }
    set enablePick(value: boolean) { this._enablePick.value = value; }

    
    // 单体化信息
    private _monomerInfo = this.dv(reactJson<any>([]));
    get monomerInfo() { return this._monomerInfo.value; }
    get monomerInfoChanged() { return this._monomerInfo.changed; }
    set monomerInfo(value: boolean) { this._monomerInfo.value = value; }

    // 拾取处理
    private _pickResettting = this.dv(new ObjResettingWithEvent(this.enablePickChanged, () => {
        if (!this.enablePick) return undefined;
        return new PickResettting(this, this.owner);
    }))
    get pickResettting() { return this._pickResettting; }

    get eSMsTileset () {
        return this.owner.modelser2Manager.eSMsTileset;
    }

    // 根据经纬度获取单体化
    async getMonomer(position: [number, number, number]) {
        try {
            const param = {
                "tileSerivceName": this.eSMsTileset.tileServiceName,
                "position": position
              }
            const res = await reqGetMonomer(param)
            // @ts-ignore
            if (res.status != 'ok') return;
            this.monomerInfo = res.data;
            console.log("res",res);
            
        } catch (error) {
            console.error(error);
        }
    }

    init() {
        this.enablePick = true
    }
    clear() {
        this.enablePick = false
    }

    constructor(owner: MyProjectManager) {
        super(owner);
    }
}







